/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.JPanel;

import controllers.entity.CanBo;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import models.CanBoDAO;
import models.DBConnect;

/**
 *
 * @author HoaDuyen
 */
public class Tim_kiem extends javax.swing.JPanel {

    /**
     * Creates new form Tim_kiem
     */
    private JFrame window;
    private CanBo canbo;
    private DefaultTableModel tblModel;
    public Tim_kiem(JFrame window) {
        tblModel = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int row, int column) {
            //all cells false
            return false;
        }
        };
        for (int i = 0 ; i< tblModel.getRowCount(); i++)
            tblModel.removeRow(0);
        tblModel = new DefaultTableModel();
        this.window=window;
        initComponents();
       
        
    }
     public void Show()
    {
        this.setBounds(160, 0, 800, 600);
        window.add(this);
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tencanbo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        chucdanhs = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        chucvus = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        bsearch = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        goitinh = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        dangviencb = new javax.swing.JComboBox();
        doanviencb = new javax.swing.JComboBox();
        congdoanviencb = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblData = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setBackground(new java.awt.Color(102, 204, 255));
        setPreferredSize(new java.awt.Dimension(800, 600));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel2.setText("Tìm Kiếm");

        jPanel1.setBackground(new java.awt.Color(102, 204, 255));

        jLabel1.setText("Tên cán bộ");

        jLabel5.setText("Chức danh ");

        jLabel9.setText("Chức vụ");

        bsearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_search.png"))); // NOI18N
        bsearch.setText("Tìm Kiếm");
        bsearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bsearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5)
                            .addComponent(jLabel9))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(43, 43, 43)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(chucvus)
                                    .addComponent(tencanbo, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(chucdanhs, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(111, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tencanbo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(chucdanhs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chucvus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(bsearch))
        );

        jPanel2.setBackground(new java.awt.Color(102, 204, 255));

        jLabel3.setText("Giới tính");

        goitinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tất cả", "Nam", "Nữ" }));

        jLabel7.setText("Đảng viên");

        jLabel8.setText("Đoàn viên");

        dangviencb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tất cả", "Có", "Không", " " }));

        doanviencb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tất cả", "Có", "Không" }));

        congdoanviencb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tất cả", "Có", "Không" }));

        jLabel10.setText("Công đoàn viên");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(congdoanviencb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(doanviencb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(goitinh, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(dangviencb, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(72, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(goitinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(dangviencb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(doanviencb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(congdoanviencb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jScrollPane1.setEnabled(false);

        tblData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Họ Tên", "Số Điện Thoại", "Chức Danh", "Chức Vụ", "Khoa Viện"
            }
        ));
        tblData.setEnabled(false);
        jScrollPane1.setViewportView(tblData);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/employee.png"))); // NOI18N
        jButton1.setText("Xem thông tin");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(118, 118, 118))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(312, 312, 312)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(328, 328, 328)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 772, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54)
                .addComponent(jButton1)
                .addContainerGap(91, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bsearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bsearchActionPerformed
       DBConnect.Connect();
       tblData.setEnabled(true);
        CanBoDAO canbodao = new CanBoDAO();
        String tenCB=tencanbo.getText(); 
        String gioitinh;
        boolean dangvien,congdoanvien;
        String chucvu=chucvus.getText();
        String chucdanh=chucdanhs.getText();
        //lay gia tri cac bien
        for (int i = 0 ; i< tblModel.getRowCount(); i++)
            tblModel.removeRow(0);
        tblModel = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int row, int column) {
            //all cells false
            return false;
        }
        };
        tblModel.addColumn("Mã Cán Bộ");
        tblModel.addColumn("Họ Tên");
        tblModel.addColumn("Số Điện Thoại");
        tblModel.addColumn("Chức Danh");
        tblModel.addColumn("Chức Vụ");
        tblModel.addColumn("Khoa Viện");
        if (goitinh.getSelectedItem().equals("Nam")) gioitinh="Nam"; 
        else if (goitinh.getSelectedItem().equals("Nữ")) gioitinh="Nữ";
        else gioitinh="";
        int dv1,dv2,cdv1,cdv2,dov1,dov2,cd1,cd2;;
        if (dangviencb.getSelectedItem().equals("Có")) {
            dv1 = 1; dv2 = -1;
        } else if (dangviencb.getSelectedItem().equals("Không")) {dv1 = 0; dv2 = -1;}
        else  {dv1 = 0; dv2 = 1;};
        
        if (doanviencb.getSelectedItem().equals("Có")) {dov1 = 1; dov2 = -1;} 
        else if (doanviencb.getSelectedItem().equals("Không")) {dov1 = 0; dov2 = -1;}
        else  {dov1 = 0; dov2 = 1;};
         if (congdoanviencb.getSelectedItem().equals("Có")) {cd1 = 1; cd2 = -1;} 
        else if (congdoanviencb.getSelectedItem().equals("Không")) {cd1 = 0; cd2 = -1;}
        else  {cd1 = 0; cd2 = 1;};
        List<CanBo> canboLis = canbodao.getSearchCanBo(tenCB,chucdanh,chucvu,gioitinh,dv1,dv2,dov1,dov2,cd1,cd2);
        if (canboLis.isEmpty()) JOptionPane.showMessageDialog(this, "Không tìm thấy ! ");
        boolean ok=true;
        for (CanBo cb: canboLis){
            loadTableData(cb);
            ok=false;
            tblData.setModel(tblModel);
        }
        if (ok)tblData.setModel(tblModel);
            
    }//GEN-LAST:event_bsearchActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int dem = tblData.getSelectedRow();
        if (dem == -1) {
            JOptionPane.showMessageDialog(this,"Chọn cán bộ cần xem thông tin trước !");
            return;
        }
          
        TableModel tm = tblData.getModel();
        Object value = tm.getValueAt(dem, 0);
        int macanbo=Integer.parseInt(value.toString());
        window.setEnabled(false);
        TTCanBo canboP=new TTCanBo(this,window,macanbo);
        canboP.setVisible(true);
        this.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed
    private void loadTableData(CanBo category) {
                Vector<String> row = new Vector<>();
                row.addElement(String.valueOf(category.getMaCanBo()));
                row.addElement(String.valueOf(category.getHoTen()));
                row.addElement(String.valueOf(category.getSoDienThoai()));
                row.addElement(String.valueOf(category.getChucDanh()));
                row.addElement(String.valueOf(category.getChucVu()));
                row.addElement(String.valueOf(category.getKhoaVien()));
                tblModel.addRow(row);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bsearch;
    private javax.swing.JTextField chucdanhs;
    private javax.swing.JTextField chucvus;
    private javax.swing.JComboBox congdoanviencb;
    private javax.swing.JComboBox dangviencb;
    private javax.swing.JComboBox doanviencb;
    private javax.swing.JComboBox goitinh;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblData;
    private javax.swing.JTextField tencanbo;
    // End of variables declaration//GEN-END:variables
}
